package com.hairline.controller;

import com.hairline.bean.User;
import com.hairline.bean.bo.LoginBO;
import com.hairline.bean.vo.AuthLoginVO;
import com.hairline.bean.vo.BaseRespVo;
import com.hairline.bean.vo.UserInfoVO;
import com.hairline.service.UserService;
import com.hairline.service.WxAuthService;
import com.hairline.shiro.MallToken;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@SuppressWarnings("ALL")
@RestController
@RequestMapping("wx/auth")
public class WxAuthController {

    @Autowired
    WxAuthService wxAuthService;

    @Autowired
    UserService userService;

    @RequestMapping("login")
    public BaseRespVo authLogin(@RequestBody LoginBO loginBO, HttpServletRequest request) {
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(new MallToken(loginBO.getUsername(), loginBO.getPassword(), "wxrealm"));
        } catch (Exception e) {
            return BaseRespVo.fail("账号或密码错误");
        }

        AuthLoginVO authLoginVO = new AuthLoginVO();
        Session session = subject.getSession();
        authLoginVO.setToken(session.getId());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        authLoginVO.setTokenExpire(format.format(calendar.getTime()));
        User user = (User) subject.getPrincipals().getPrimaryPrincipal();
        UserInfoVO userInfoVO = new UserInfoVO(user.getNickname(), user.getAvatar());
        authLoginVO.setUserInfo(userInfoVO);

        //更新最近登录时间和ip
        userService.updateLoginTime(user.getId(), request.getRemoteAddr());

        return BaseRespVo.ok(authLoginVO);
    }

    @RequestMapping("login_by_weixin")
    public BaseRespVo authLoginByWeixin(HttpServletRequest request) {
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(new MallToken("test1", "test1", "wxrealm"));
        } catch (Exception e) {
            return BaseRespVo.fail("账号或密码错误");
        }

        AuthLoginVO authLoginVO = new AuthLoginVO();
        Session session = subject.getSession();
        authLoginVO.setToken(session.getId());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        authLoginVO.setTokenExpire(format.format(calendar.getTime()));
        User user = (User) subject.getPrincipals().getPrimaryPrincipal();
        UserInfoVO userInfoVO = new UserInfoVO(user.getNickname(), user.getAvatar());
        authLoginVO.setUserInfo(userInfoVO);

        //更新最近登录时间和ip
        userService.updateLoginTime(user.getId(), request.getRemoteAddr());

        return BaseRespVo.ok(authLoginVO);
    }

    @RequestMapping("logout")
    public BaseRespVo authLogout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return BaseRespVo.ok();
    }

    @RequestMapping("needLogin")
    public BaseRespVo needLogin() {
        return BaseRespVo.fail("请登录", 501L);
    }
}
